100 improve community backend query performance #121
Merged
+1,681
−31
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Purpose
The Community page triggered one extra database query per published build to load its components, an N+1 problem: showing 20 builds took about 21 queries, growing with the number of builds. This PR batches those lookups so the query count stays constant.
Changes
createBuildPartsLoaderinresolvers.ts) that fetches the parts of all requested builds in one parameterized query.index.ts, so results are not cached between requests.dataloaderdependency.buildPartsBatching.test.ts, which runs the realbuilds { parts }query through Apollo and checks the results and the number of queries. Update theBuild.partstest inresolvers.test.tsto use the loader.Loading 20 builds with their parts now takes 2 queries (one for the builds, one for all their parts) instead of 21.
Related issues
Closes #100
Verification
npm test: all 85 tests pass. The new tests show exactly 2 queries for 1, 5, 20 and 100 builds, that every build gets its own parts in category order, that shared parts are fetched once, and that no parts query runs when no build has parts.npx tsc --noEmitandnpx eslint .: clean.Screenshots
Not applicable
Checklist